'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports System.IO

''' <summary>
''' Provides access to Services shared accross the application.
''' </summary>
Public NotInheritable Class ApplicationService
    Private Sub New()

    End Sub

    Private Shared _Current As ApplicationService

    Private _DataStorageDirectory As DirectoryInfo
    Private _Context As IcqInterface.Interfaces.IContext
    Private _IdentityProvider As IdentityManager.XmlIdentityProvider
    Private _Identity As IdentityManager.IIdentity

    ''' <summary>
    ''' Gets the current ApplicationService.
    ''' </summary>
    ''' <remarks>Current is available after calling Initialize.</remarks>
    Public Shared ReadOnly Property Current() As ApplicationService
        Get
            Return _Current
        End Get
    End Property

    ''' <summary>
    ''' Gets the directory where application data is stored.
    ''' </summary>
    Public ReadOnly Property DataStorageDirectory() As DirectoryInfo
        Get
            Return _DataStorageDirectory
        End Get
    End Property

    ''' <summary>
    ''' Gets the current IContext.
    ''' </summary>
    Public ReadOnly Property Context() As IcqInterface.Interfaces.IContext
        Get
            Return _Context
        End Get
    End Property

    Public Shared ReadOnly Property CurrentContext() As IcqInterface.Interfaces.IContext
        Get
            Return Current.Context
        End Get
    End Property

    ''' <summary>
    ''' Gets the current IIdentityProvider.
    ''' </summary>
    Public ReadOnly Property IdentityProvider() As IdentityManager.IIdentityProvider
        Get
            Return _IdentityProvider
        End Get
    End Property

    ''' <summary>
    ''' Gets the current Identity.
    ''' </summary>
    Public ReadOnly Property Identity() As IdentityManager.IIdentity
        Get
            Return _Identity
        End Get
    End Property

    ''' <summary>
    ''' Initializes an ApplicationService for the current application.
    ''' </summary>
    ''' <param name="dataStorage">The directory where application data is stored.</param>
    Public Shared Sub Initialize(ByVal dataStorage As System.IO.DirectoryInfo)
        Dim xmlIdentities As System.IO.FileInfo

        xmlIdentities = New System.IO.FileInfo(System.IO.Path.Combine(dataStorage.FullName, "identitystore.xml"))

        _Current = New ApplicationService
        _Current._DataStorageDirectory = dataStorage
        _Current._IdentityProvider = New IdentityManager.XmlIdentityProvider(xmlIdentities)
    End Sub

    Public Function CreateContext(ByVal identity As IdentityManager.IIdentity) As IcqInterface.Interfaces.IContext
        _Identity = identity
        _Context = New IcqInterface.IcqContext(identity.GetAttribute(IdentityAttributes.UinAttribute))

        LoadContextData(_Context)

        Return _Context
    End Function

    Private Sub LoadContextData(ByVal ctx As IcqInterface.Interfaces.IContext)
        ctx.GetService(Of IcqInterface.Interfaces.IDataWarehouseService).Load(DataStorageDirectory)
        ctx.GetService(Of IContactHistoryService).Load()
    End Sub

    Private Sub SaveContextData(ByVal ctx As IcqInterface.Interfaces.IContext)
        ctx.GetService(Of IcqInterface.Interfaces.IDataWarehouseService).Save(DataStorageDirectory)
        ctx.GetService(Of IContactHistoryService).Save()
    End Sub

    Public Sub LoadServiceData()
        _IdentityProvider.Load()
    End Sub

    Public Sub SaveServiceData()
        If Context IsNot Nothing Then SaveContextData(Context)

        _IdentityProvider.Save()
    End Sub
End Class
